﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskManageDemo.Data
{
    public class SessionFactory
    {
        /// <summary>
        /// 根据Provider类型，创建数据库连接
        /// </summary>
        /// <returns></returns>
        private static IDbConnection CreateConnectionByProvider()
        {
            var connStringSetting = ConfigurationManager.ConnectionStrings["connectionString"];
            IDbConnection conn = new SqlConnection(connStringSetting.ConnectionString);

            //IDbConnection conn = new OracleConnection(connStringSetting.ConnectionString);

            return conn;
        }


        /// <summary>
        /// 创建数据库连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection CreateConnection()
        {
            IDbConnection conn = CreateConnectionByProvider();

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }

        /// <summary>
        /// 创建数据库连接会话
        /// </summary>
        /// <returns></returns>
        public static IDbSession CreateSession()
        {
            IDbConnection conn = CreateConnection();
            IDbSession session = new DbSession(conn);

            return session;
        }

    }
}
